<?php
/**
 *   ./////////////////////////////.
 *  //////////////////////////////////
 *  ///////////    ///////////////////
 *  ////////     /////////////////////
 *  //////    ////////////////////////
 *  /////    /////////    ////////////
 *  //////     /////////     /////////
 *  /////////     /////////    ///////
 *  ///////////    //////////    /////
 *  ////////////////////////    //////
 *  /////////////////////    /////////
 *  ///////////////////    ///////////
 *  //////////////////////////////////
 *   `//////////////////////////////`
 *
 *
 * Plugin Name:         Jigoshop
 * Plugin URI:          http://jigoshop.com/
 * Description:         Jigoshop, a WordPress eCommerce plugin that works.
 * Author:              Jigowatt
 * Author URI:          http://jigowatt.co.uk
 *
 * Version:             1.2.3
 * Requires at least:   3.2.1
 * Tested up to:        3.4-beta3
 *
 * Text Domain:         jigoshop
 * Domain Path:         /languages/
 *
 * DISCLAIMER
 *
 * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
 * versions in the future. If you wish to customise Jigoshop core for your needs,
 * please use our GitHub repository to publish essential changes for consideration.
 *
 * @package             Jigoshop
 * @category            Core
 * @author              Jigowatt
 * @copyright           Copyright © 2011-2012 Jigowatt Ltd.
 * @license             http://jigoshop.com/license/commercial-edition
 */

if (!defined("JIGOSHOP_VERSION")) define("JIGOSHOP_VERSION", 1203310);
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

// Load administration & check if we need to install
if ( is_admin() ) {
	include_once( 'admin/jigoshop-admin.php' );
	register_activation_hook( __FILE__, 'install_jigoshop' );
}

/**
 * Include core files and classes
 **/
include_once( 'classes/abstract/jigoshop_base.class.php' );
include_once( 'classes/abstract/jigoshop_singleton.php' );
include_once( 'classes/jigoshop_session.class.php' );
include_once( 'classes/jigoshop_sanitize.class.php' );
include_once( 'classes/jigoshop_validation.class.php' );
include_once( 'jigoshop_taxonomy.php' );

include_once( 'classes/jigoshop_countries.class.php' );
include_once( 'classes/jigoshop_customer.class.php' );
include_once( 'classes/jigoshop_product.class.php' );
include_once( 'classes/jigoshop_product_variation.class.php' );
include_once( 'classes/jigoshop_order.class.php' );
include_once( 'classes/jigoshop_orders.class.php' );
include_once( 'classes/jigoshop_tax.class.php' );
include_once( 'classes/jigoshop_shipping.class.php' );
include_once( 'classes/jigoshop_coupons.class.php' );

include_once( 'gateways/gateways.class.php' );
include_once( 'gateways/gateway.class.php' );
include_once( 'gateways/bank_transfer.php' );
include_once( 'gateways/cheque.php' );
include_once( 'gateways/cod.php' );
include_once( 'gateways/dibs.php' );
include_once( 'gateways/paypal.php' );
include_once( 'gateways/skrill.php' );

include_once( 'shipping/shipping_method.class.php' );
include_once( 'shipping/jigoshop_calculable_shipping.php' );
include_once( 'shipping/flat_rate.php' );
include_once( 'shipping/free_shipping.php' );
include_once( 'shipping/local_pickup.php' );

include_once( 'classes/jigoshop_query.class.php' );
include_once( 'classes/jigoshop.class.php' );
include_once( 'classes/jigoshop_cart.class.php' );
include_once( 'classes/jigoshop_checkout.class.php' );

include_once( 'shortcodes/init.php' );
include_once( 'widgets/init.php' );

include_once( 'jigoshop_templates.php' );
include_once( 'jigoshop_template_actions.php' );
include_once( 'jigoshop_emails.php' );
include_once( 'jigoshop_actions.php' );
//include_once( 'jigoshop_cron.php' );	/* we may use this at some point, leaving -JAP- */

// Constants
if (!defined('JIGOSHOP_USE_CSS')) :
	if (get_option('jigoshop_disable_css')=='yes') define('JIGOSHOP_USE_CSS', false);
	else define('JIGOSHOP_USE_CSS', true);
endif;
if (!defined('JIGOSHOP_LOAD_FANCYBOX')) :
	if (get_option('jigoshop_disable_fancybox')=='yes') define('JIGOSHOP_LOAD_FANCYBOX', false);
	else define('JIGOSHOP_LOAD_FANCYBOX', true);
endif;
if ( !defined('JIGOSHOP_TEMPLATE_URL') ) define('JIGOSHOP_TEMPLATE_URL', 'jigoshop/');

/**
 * IIS compat fix/fallback
 **/
if (!isset($_SERVER['REQUEST_URI'])) {
	$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
	if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING']; }
}

/**
 *  Jigoshop Intialization
 */
function jigoshop_init() {

	// Override default translations with custom .mo's found in wp-content/languages/jigoshop
	load_textdomain( 'jigoshop', WP_LANG_DIR.'/jigoshop/jigoshop-'.get_locale().'.mo' );
	load_plugin_textdomain( 'jigoshop', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

	/* ensure nothing is output to the browser prior to this (other than headers) */
	ob_start();

	jigoshop_post_type();                   // register taxonomies

	// add Singletons here so that the taxonomies are loaded before calling them.
	jigoshop_session::instance();           // Session management, start them if not running
	jigoshop::instance();                   // Utility functions
	jigoshop_customer::instance();          // Customer class, sorts session data such as location
	jigoshop_shipping::instance();          // Shipping class. loads shipping methods
	jigoshop_payment_gateways::instance();  // Payment gateways class. loads payment methods
	jigoshop_cart::instance();              // Cart class, stores the cart contents

	if ( ! is_admin() ) jigoshop_catalog_query::instance();

	// Image sizes
	jigoshop_set_image_sizes();

	// Include template functions here so they are pluggable by themes
	include_once( 'jigoshop_template_functions.php' );

	add_role('customer', 'Customer', array(
		'read'        => true,
		'edit_posts'  => false,
		'delete_posts'=> false
	));

}
add_action('init', 'jigoshop_init', 0);

// Add a "Settings" link to the plugins.php page for Jigoshop
function jigoshop_add_settings_link( $links, $file ) {
	$this_plugin = plugin_basename( __FILE__ );
	if( $file == $this_plugin ) {
		$settings_link = '<a href="admin.php?page=jigoshop_settings">' . __( 'Settings', 'jigoshop' ) . '</a>';
		array_unshift($links, $settings_link);
	}
	return $links;
}
add_filter( 'plugin_action_links', 'jigoshop_add_settings_link', 10, 2 );

/**
 * Add post thumbnail support to WordPress if needed
 **/
function jigoshop_check_thumbnail_support() {
	if ( ! current_theme_supports( 'post-thumbnails' ) ) {
		add_theme_support( 'post-thumbnails' );
		remove_post_type_support( 'post', 'thumbnail' );
		remove_post_type_support( 'page', 'thumbnail' );
	} else {
		add_post_type_support( 'product', 'thumbnail' );
	}
}
add_action( 'after_setup_theme', 'jigoshop_check_thumbnail_support', 99 );

/**
 * Mail from name/email
 **/
function jigoshop_mail_from_name( $name ) {
	$name = get_bloginfo('name');
	$name = esc_attr($name);
	return $name;
}
add_filter( 'wp_mail_from_name', 'jigoshop_mail_from_name' );

/*
function jigoshop_mail_from( $email ) {
	$email = get_option('jigoshop_email');
	return $email;
}
add_filter( 'wp_mail_from', 'jigoshop_mail_from' );
*/

//### Functions #########################################################

/**
 * Set Jigoshop Product Image Sizes for WordPress based on Admin->Jigoshop->Settings->Images
 * @since 0.9.9
 **/
function jigoshop_set_image_sizes() {
	$tinycrop = get_option( 'jigoshop_use_wordpress_tiny_crop', 'no' ) == 'yes' ? true : false;
	$thumbcrop = get_option( 'jigoshop_use_wordpress_thumbnail_crop', 'no' ) == 'yes' ? true : false;
	$catalogcrop = get_option( 'jigoshop_use_wordpress_catalog_crop', 'no' ) == 'yes' ? true : false;
	$featuredcrop = get_option( 'jigoshop_use_wordpress_featured_crop', 'no' ) == 'yes' ? true : false;

	add_image_size( 'admin_product_list', 32, 32, $tinycrop );
	add_image_size( 'shop_tiny', get_option('jigoshop_shop_tiny_w'), get_option('jigoshop_shop_tiny_h'), $tinycrop );
	add_image_size( 'shop_thumbnail', get_option('jigoshop_shop_thumbnail_w'), get_option('jigoshop_shop_thumbnail_h'), $thumbcrop );
	add_image_size( 'shop_small', get_option('jigoshop_shop_small_w'), get_option('jigoshop_shop_small_h'), $catalogcrop );
	add_image_size( 'shop_large', get_option('jigoshop_shop_large_w'), get_option('jigoshop_shop_large_h'), $featuredcrop );
}

/**
 * Get Jigoshop Product Image Size based on Admin->Jigoshop->Settings->Images
 * @param string $size - one of the 4 defined Jigoshop image sizes
 * @return array - an array containing the width and height of the required size
 * @since 0.9.9
 **/
function jigoshop_get_image_size( $size ) {

	if ( is_array( $size ) )
		return $size;

	switch ( $size ) :
		case 'admin_product_list':
			$image_size = array( 32, 32 );
			break;
		case 'shop_tiny':
			$image_size = array( get_option('jigoshop_shop_tiny_w'), get_option('jigoshop_shop_tiny_h') );
			break;
		case 'shop_thumbnail':
			$image_size = array( get_option('jigoshop_shop_thumbnail_w'), get_option('jigoshop_shop_thumbnail_h') );
			break;
		case 'shop_small':
			$image_size = array( get_option('jigoshop_shop_small_w'), get_option('jigoshop_shop_small_h') );
			break;
		case 'shop_large':
			$image_size = array( get_option('jigoshop_shop_large_w'), get_option('jigoshop_shop_large_h') );
			break;
		default:
			$image_size = array( get_option('jigoshop_shop_small_w'), get_option('jigoshop_shop_small_h') );
			break;
	endswitch;

	return $image_size;
}

function jigoshop_is_admin_page() {

	global $current_screen;

	if ( $current_screen->post_type == 'product' || $current_screen->post_type == 'shop_order' )
		return $current_screen->post_type;

	if ( strstr( $current_screen->id, 'jigoshop' ) )
		return $current_screen->id;

	return false;

}

add_action( 'admin_enqueue_scripts', 'jigoshop_admin_styles' );
function jigoshop_admin_styles() {

	/* Our setting icons */
	wp_enqueue_style( 'jigoshop_admin_icons_style', jigoshop::assets_url() . '/assets/css/admin-icons.css' );

	if ( ! jigoshop_is_admin_page() ) return false;
	wp_enqueue_style( 'jigoshop_admin_styles', jigoshop::assets_url() . '/assets/css/admin.css' );
	wp_enqueue_style( 'jquery-ui-jigoshop-styles', jigoshop::assets_url() . '/assets/css/jquery-ui-1.8.16.jigoshop.css' );

}

function jigoshop_disable_autosave( $src, $handle ) {
    if( 'autosave' != $handle ) return $src;
    return '';
}

function jigoshop_admin_scripts() {

	if (!jigoshop_is_admin_page()) return false;

	$pagenow = jigoshop_is_admin_page();

	wp_enqueue_script('jigoshop_backend'    , jigoshop::assets_url() . '/assets/js/jigoshop_backend.js'               , array( 'jquery' ), '1.0' );
	wp_enqueue_script('jquery-ui-datepicker', jigoshop::assets_url() . '/assets/js/jquery-ui-datepicker-1.8.16.min.js', array( 'jquery' ), '1.8.16', true );

	/**
	 * Disable autosaves on the order pages. Prevents the javascript alert when modifying an order.
	 * `wp_deregister_script( 'autosave' )` would produce errors, so we use a filter instead.
	 */

	if ( $pagenow == 'shop_order' )
		add_filter( 'script_loader_src', 'jigoshop_disable_autosave', 10, 2 );

}
add_action('admin_print_scripts', 'jigoshop_admin_scripts');

function jigoshop_frontend_scripts() {

	if ( ! is_jigoshop() && is_admin() ) return false;
	$css = file_exists(get_stylesheet_directory() . '/jigoshop/style.css') ? get_stylesheet_directory_uri() . '/jigoshop/style.css' : jigoshop::assets_url() . '/assets/css/frontend.css';

	/* Frontend scripts */
	if( JIGOSHOP_LOAD_FANCYBOX )
	wp_enqueue_script( 'fancybox'              , jigoshop::assets_url() . '/assets/js/jquery.fancybox-1.3.4.pack.js'     , array('jquery'), '1.0' );
	wp_enqueue_script( 'jigoshop_frontend'     , jigoshop::assets_url() . '/assets/js/jigoshop_frontend.js'              , array('jquery'), '1.0' );
	wp_enqueue_script( 'jigoshop_script'       , jigoshop::assets_url() . '/assets/js/script.js'                         , array('jquery'), '1.0' );
	wp_enqueue_script( 'jqueryui'              , 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js', array('jquery'), '1.0' );

	/* Frontend styles */
	if (JIGOSHOP_USE_CSS)
	wp_enqueue_style('jigoshop_frontend_styles', $css );
	if( JIGOSHOP_LOAD_FANCYBOX )
	wp_enqueue_style('jigoshop_fancybox_styles', jigoshop::assets_url() . '/assets/css/fancybox.css');
	wp_enqueue_style('jqueryui_styles'         , jigoshop::assets_url() . '/assets/css/ui.css');

	/* Script.js variables */
	$params = array(
		'ajax_url' 						=> (!is_ssl()) ? str_replace('https', 'http', admin_url('admin-ajax.php')) : admin_url('admin-ajax.php'),
		'assets_url' 					=> jigoshop::assets_url(),
		'checkout_url'					=> admin_url('admin-ajax.php?action=jigoshop-checkout'),
		'countries' 					=> json_encode(jigoshop_countries::$states),
		'currency_symbol' 				=> get_jigoshop_currency_symbol(),
		'get_variation_nonce' 			=> wp_create_nonce("get-variation"),
		'load_fancybox'					=> JIGOSHOP_LOAD_FANCYBOX,
		'option_guest_checkout'			=> get_option('jigoshop_enable_guest_checkout'),
		'select_state_text' 			=> __('Select a state&hellip;', 'jigoshop'),
		'state_text' 					=> __('state', 'jigoshop'),
		'update_order_review_nonce' 	=> wp_create_nonce("update-order-review"),
        'billing_state'                 => jigoshop_customer::get_state(),
        'shipping_state'                => jigoshop_customer::get_shipping_state()
	);

	if (isset( jigoshop_session::instance()->min_price ))
		$params['min_price'] = $_GET['min_price'];

	if (isset( jigoshop_session::instance()->max_price ))
		$params['max_price'] = $_GET['max_price'];

	$params['is_checkout'] = ( is_page(jigoshop_get_page_id('checkout')) || is_page(jigoshop_get_page_id('pay')) )
						   ? 1
						   : 0;

	$params = apply_filters('jigoshop_params', $params);

	wp_localize_script( 'jigoshop_script', 'params', $params );

}
add_action('template_redirect', 'jigoshop_frontend_scripts');


/*
	jigoshop_demo_store
	Adds a demo store banner to the site
*/
function jigoshop_demo_store() {

	if ( get_option('jigoshop_demo_store')=='yes' && is_jigoshop() ) :

		echo '<p class="demo_store">'.__('This is a demo store for testing purposes &mdash; no orders shall be fulfilled.', 'jigoshop').'</p>';

	endif;
}
add_action( 'wp_footer', 'jigoshop_demo_store' );

/**
 * Allow product_cat in the permalinks for products.
 *
 * @param string $permalink The existing permalink URL.
 */
add_filter( 'post_type_link', 'jigoshop_product_cat_filter_post_link', 10, 4 );

function jigoshop_product_cat_filter_post_link( $permalink, $post, $leavename, $sample ) {

    if ($post->post_type!=='product') return $permalink;

    // Abort early if the placeholder rewrite tag isn't in the generated URL
    if ( false === strpos( $permalink, '%product_cat%' ) ) return $permalink;

    // Get the custom taxonomy terms in use by this post
    $terms = get_the_terms( $post->ID, 'product_cat' );

    if ( empty( $terms ) ) :
    	// If no terms are assigned to this post, use a string instead
        $permalink = str_replace( '%product_cat%', _x('product', 'slug', 'jigoshop'), $permalink );
    else :
    	// Replace the placeholder rewrite tag with the first term's slug
        $first_term = array_shift( $terms );
        $permalink = str_replace( '%product_cat%', $first_term->slug, $permalink );
    endif;

    return $permalink;
}

/*
	jigoshop_sharethis
	Adds social sharing code to footer
*/
function jigoshop_sharethis() {
	if (is_single() && get_option('jigoshop_sharethis')) :

		if (is_ssl()) :
			$sharethis = 'https://ws.sharethis.com/button/buttons.js';
		else :
			$sharethis = 'http://w.sharethis.com/button/buttons.js';
		endif;

		echo '<script type="text/javascript">var switchTo5x=true;</script><script type="text/javascript" src="'.$sharethis.'"></script><script type="text/javascript">stLight.options({publisher:"'.get_option('jigoshop_sharethis').'"});</script>';

	endif;
}
add_action( 'wp_footer', 'jigoshop_sharethis' );

/**
 * Evaluates to true only on the Shop page, not Product categories and tags
 * Note:is used to replace is_page( jigoshop_get_page_id( 'shop' ) )
 *
 * @return bool
 * @since 0.9.9
 */
function is_shop() {
	return is_post_type_archive( 'product' ) || is_page( jigoshop_get_page_id('shop') );
}

/**
 * Evaluates to true only on the Category Pages
 *
 * @return bool
 * @since 0.9.9
 */
function is_product_category() {
	return is_tax( 'product_cat' );
}

/**
 * Evaluates to true only on the Tag Pages
 *
 * @return bool
 * @since 0.9.9
 */
function is_product_tag() {
	return is_tax( 'product_tag' );
}

/**
 * Evaluates to true only on the Single Product Page
 *
 * @return bool
 * @since 0.9.9
 */
function is_product() {
	return is_singular( array('product') );
}

/**
 * Evaluates to true only on Shop, Product Category, and Product Tag pages
 *
 * @return bool
 * @since 0.9.9
 */
function is_product_list() {
	$is_list = false;
	$is_list |= is_shop();
	$is_list |= is_product_tag();
	$is_list |= is_product_category();
	return $is_list;
}

/**
 * Evaluates to true for all Jigoshop pages
 *
 * @return bool
 * @since 0.9.9
 */
function is_jigoshop() {
	$is_jigo = false;
	$is_jigo |= is_content_wrapped();
	$is_jigo |= is_account();
	$is_jigo |= is_cart();
	$is_jigo |= is_checkout();
	$is_jigo |= is_order_tracker();
	return $is_jigo;
}

/**
 * Evaluates to true only on the Shop, Category, Tag and Single Product Pages
 *
 * @return bool
 * @since 0.9.9.1
 */
function is_content_wrapped() {
	$is_wrapped = false;
	$is_wrapped |= is_product_list();
	$is_wrapped |= is_product();
	return $is_wrapped;
}

/**
 * Jigoshop page IDs
 *
 * returns -1 if no page is found
 **/
if (!function_exists('jigoshop_get_page_id')) {
	function jigoshop_get_page_id( $page ) {
		$page = apply_filters('jigoshop_get_' . $page . '_page_id', get_option('jigoshop_' . $page . '_page_id'));
		return ($page) ? $page : -1;
	}
}

/**
 * Evaluates to true only on the Order Tracking page
 *
 * @return bool
 * @since 0.9.9.1
 */
function is_order_tracker() {
	return is_page( jigoshop_get_page_id('track_order'));
}

/**
 * Evaluates to true only on the Cart page
 *
 * @return bool
 * @since 0.9.8
 */
function is_cart() {
	return is_page( jigoshop_get_page_id('cart'));
}

/**
 * Evaluates to true only on the Checkout or Pay pages
 *
 * @return bool
 * @since 0.9.8
 */
function is_checkout() {
	return is_page( jigoshop_get_page_id('checkout')) | is_page( jigoshop_get_page_id('pay'));
}

/**
 * Evaluates to true only on the main Account or any sub-account pages
 *
 * @return bool
 * @since 0.9.9.1
 */
function is_account() {
	$is_account = false;
	$is_account |= is_page( jigoshop_get_page_id('myaccount') );
	$is_account |= is_page( jigoshop_get_page_id('edit_address') );
	$is_account |= is_page( jigoshop_get_page_id('change_password') );
	$is_account |= is_page( jigoshop_get_page_id('view_order') );
	return $is_account;
}

if (!function_exists('is_ajax')) {
	function is_ajax() {
		if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) return true;
		return false;
	}
}

function jigoshop_force_ssl() {
	if (is_checkout() && !is_ssl()) :
		wp_safe_redirect( str_replace('http:', 'https:', get_permalink(jigoshop_get_page_id('checkout'))), 301 );
		exit;
	endif;
}
if (!is_admin() && get_option('jigoshop_force_ssl_checkout')=='yes') add_action( 'wp', 'jigoshop_force_ssl');

function jigoshop_force_ssl_images( $content ) {
	if (is_ssl()) :
		if (is_array($content)) :
			$content = array_map('jigoshop_force_ssl_images', $content);
		else :
			$content = str_replace('http:', 'https:', $content);
		endif;
	endif;
	return $content;
}
add_filter('post_thumbnail_html', 'jigoshop_force_ssl_images');
add_filter('widget_text', 'jigoshop_force_ssl_images');
add_filter('wp_get_attachment_url', 'jigoshop_force_ssl_images');
add_filter('wp_get_attachment_image_attributes', 'jigoshop_force_ssl_images');
add_filter('wp_get_attachment_url', 'jigoshop_force_ssl_images');

function jigoshop_force_ssl_urls( $url ) {
	if (is_ssl()) :
		$url = str_replace('http:', 'https:', $url);
	endif;
	return $url;
}
add_filter('option_siteurl', 'jigoshop_force_ssl_urls');
add_filter('option_home', 'jigoshop_force_ssl_urls');
add_filter('option_url', 'jigoshop_force_ssl_urls');
add_filter('option_wpurl', 'jigoshop_force_ssl_urls');
add_filter('option_stylesheet_url', 'jigoshop_force_ssl_urls');
add_filter('option_template_url', 'jigoshop_force_ssl_urls');
add_filter('script_loader_src', 'jigoshop_force_ssl_urls');
add_filter('style_loader_src', 'jigoshop_force_ssl_urls');

// http://www.xe.com/symbols.php
function get_jigoshop_currency_symbol() {
	$currency = get_option('jigoshop_currency');
	$currency_symbol = '';
	switch ($currency) :
		case 'AED' : $currency_symbol = '&#1583;&#46;&#1573;'; break;
		case 'AUD' : $currency_symbol = '&#36;'; break;
		case 'BRL' : $currency_symbol = '&#82;&#36;'; break;
		case 'CAD' : $currency_symbol = '&#36;'; break;
		case 'CHF' : $currency_symbol = 'SFr.'; break;
		case 'CNY' : $currency_symbol = '&#165;'; break;
		case 'CZK' : $currency_symbol = '&#75;&#269;'; break;
		case 'DKK' : $currency_symbol = 'kr'; break;
		case 'EUR' : $currency_symbol = '&euro;'; break;
		case 'GBP' : $currency_symbol = '&pound;'; break;
		case 'HKD' : $currency_symbol = '&#36;'; break;
		case 'HRK' : $currency_symbol = '&#107;&#110;'; break;
		case 'HUF' : $currency_symbol = '&#70;&#116;'; break;
		case 'IDR' : $currency_symbol = '&#82;&#112;'; break;
		case 'ILS' : $currency_symbol = '&#8362;'; break;
		case 'INR' : $currency_symbol = '&#8360;'; break;
		case 'JPY' : $currency_symbol = '&yen;'; break;
		case 'MXN' : $currency_symbol = '&#36;'; break;
		case 'MYR' : $currency_symbol = 'RM'; break;
		case 'NGN' : $currency_symbol = '&#8358;'; break;
		case 'NOK' : $currency_symbol = 'kr'; break;
		case 'NZD' : $currency_symbol = '&#36;'; break;
		case 'PHP' : $currency_symbol = '&#8369;'; break;
		case 'PLN' : $currency_symbol = '&#122;&#322;'; break;
		case 'RON' : $currency_symbol = '&#108;&#101;&#105;'; break;
		case 'RUB' : $currency_symbol = '&#1088;&#1091;&#1073;'; break;
		case 'SEK' : $currency_symbol = 'kr'; break;
		case 'SGD' : $currency_symbol = '&#36;'; break;
		case 'THB' : $currency_symbol = '&#3647;'; break;
		case 'TRY' : $currency_symbol = '&#8356;'; break;
		case 'TWD' : $currency_symbol = '&#78;&#84;&#36;'; break;
		case 'USD' : $currency_symbol = '&#36;'; break;
		case 'ZAR' : $currency_symbol = 'R'; break;
		default    : $currency_symbol = '&pound;'; break;
	endswitch;
	return apply_filters('jigoshop_currency_symbol', $currency_symbol, $currency);
}

function jigoshop_price( $price, $args = array() ) {

	extract(shortcode_atts(array(
		'ex_tax_label' 	=> '0',
        'with_currency' => true
	), $args));

	$return = '';
	$price = number_format(
		(double) $price,
		(int) get_option('jigoshop_price_num_decimals'),
		get_option('jigoshop_price_decimal_sep'),
		get_option('jigoshop_price_thousand_sep')
	);

    $return = $price;

    if ($with_currency) :

        $currency_pos = get_option('jigoshop_currency_pos');
        $currency_symbol = get_jigoshop_currency_symbol();
        $currency_code = get_option('jigoshop_currency');

        switch ($currency_pos) :
            case 'left' :
                $return = $currency_symbol . $price;
            break;
            case 'left_space' :
                $return = $currency_symbol . ' ' . $price;
            break;
            case 'right' :
                $return = $price . $currency_symbol;
            break;
            case 'right_space' :
                $return = $price . ' ' . $currency_symbol;
            break;
            case 'left_code' :
                $return = $currency_code . $price;
            break;
            case 'left_code_space' :
                $return = $currency_code . ' ' . $price;
            break;
            case 'right_code' :
                $return = $price . $currency_code;
            break;
            case 'right_code_space' :
                $return = $price . ' ' . $currency_code;
            break;
            case 'code_symbol' :
                $return = $currency_code . $price . $currency_symbol;
            break;
            case 'code_symbol_space' :
                $return = $currency_code . ' ' . $price . ' ' . $currency_symbol;
            break;
            case 'symbol_code' :
                $return = $currency_symbol . $price . $currency_code;
            break;
            case 'symbol_code_space' :
                $return = $currency_symbol . ' ' . $price . ' ' . $currency_code;
            break;
        endswitch;

        // only show (ex. tax) if we are going to show the price with currency as well. Otherwise we just want the formatted price
        if ($ex_tax_label && get_option('jigoshop_calc_taxes')=='yes') $return .= __(' <small>(ex. tax)</small>', 'jigoshop');
    endif;

	return $return;
}

/** Show variation info if set */
function jigoshop_get_formatted_variation( $variation = '', $flat = false ) {
	if ($variation && is_array($variation)) :

		$return = '';

		if (!$flat) :
			$return = '<dl class="variation">';
		endif;

		$varation_list = array();

		foreach ($variation as $name => $value) :

			$name = str_replace('tax_', '', $name);

			if ( taxonomy_exists( 'pa_'.$name )) :
				$terms = get_terms( 'pa_'.$name, array( 'orderby' => 'slug', 'hide_empty' => '0' ) );
				foreach ( $terms as $term ) :
					if ( $term->slug == $value ) $value = $term->name;
				endforeach;
				$name = get_taxonomy( 'pa_'.$name )->labels->name;
				$name = jigoshop_product::attribute_label('pa_'.$name);
			endif;

			if ($flat) :
				$varation_list[] = $name.': '.$value;
			else :
				$varation_list[] = '<dt>'.$name.':</dt><dd>'.$value.'</dd>';
			endif;

		endforeach;

		if ($flat) :
			$return .= implode(', ', $varation_list);
		else :
			$return .= implode('', $varation_list);
		endif;

		if (!$flat) :
			$return .= '</dl>';
		endif;

		return $return;

	endif;
}

// Remove pingbacks/trackbacks from Comments Feed
// betterwp.net/wordpress-tips/remove-pingbackstrackbacks-from-comments-feed/
add_filter('request', 'jigoshop_filter_request');

function jigoshop_filter_request($qv)
{
	if (isset($qv['feed']) && !empty($qv['withcomments']))
	{
		add_filter('comment_feed_where', 'jigoshop_comment_feed_where');
	}
	return $qv;
}

function jigoshop_comment_feed_where($cwhere)
{
	$cwhere .= " AND comment_type != 'jigoshop' ";
	return $cwhere;
}

function jigoshop_let_to_num($v) {
    $l = substr($v, -1);
    $ret = substr($v, 0, -1);
    switch(strtoupper($l)){
    case 'P':
        $ret *= 1024;
    case 'T':
        $ret *= 1024;
    case 'G':
        $ret *= 1024;
    case 'M':
        $ret *= 1024;
    case 'K':
        $ret *= 1024;
        break;
    }
    return $ret;
}

function jigowatt_clean( $var ) {
	return strip_tags(stripslashes(trim($var)));
}

// Returns a float value
function jigoshop_sanitize_num( $var ) {
	// TODO: as it stands, it doesn't allow negative values (-JAP-)
	// should be - preg_replace("/^[^[\-\+]0-9\.]/","",$var)
	// currently only used for prices in product-data-save.php
	return strip_tags(stripslashes(floatval(preg_replace("/^[^0-9\.]/","",$var))));
}

//Author: Sergey Biryukov
// Plugin URI: http://wordpress.org/extend/plugins/allow-cyrillic-usernames/
function jigoshop_sanitize_user($username, $raw_username, $strict) {
	$username = wp_strip_all_tags( $raw_username );
	$username = remove_accents( $username );
	$username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
	$username = preg_replace( '/&.+?;/', '', $username ); // Kill entities

	if ( $strict )
		$username = preg_replace( '|[^a-z?-?0-9 _.\-@]|iu', '', $username );

	$username = trim( $username );
	$username = preg_replace( '|\s+|', ' ', $username );

	return $username;
}
add_filter('sanitize_user', 'jigoshop_sanitize_user', 10, 3);

global $jigoshop_body_classes;

function jigoshop_page_body_classes() {

	global $jigoshop_body_classes;

	$jigoshop_body_classes = (array) $jigoshop_body_classes;

	if ( is_order_tracker() ) jigoshop_add_body_class( array( 'jigoshop', 'jigoshop-tracker' ) );

	if ( is_checkout() ) jigoshop_add_body_class( array( 'jigoshop', 'jigoshop-checkout' ) );

	if ( is_cart() ) jigoshop_add_body_class( array( 'jigoshop', 'jigoshop-cart' ) );

	if ( is_page(jigoshop_get_page_id('thanks'))) jigoshop_add_body_class( array( 'jigoshop', 'jigoshop-thanks' ) );

	if ( is_page(jigoshop_get_page_id('pay'))) jigoshop_add_body_class( array( 'jigoshop', 'jigoshop-pay' ) );

	if ( is_account() ) jigoshop_add_body_class( array( 'jigoshop', 'jigoshop-myaccount' ) );

}
add_action('wp_head', 'jigoshop_page_body_classes');

function jigoshop_add_body_class( $class = array() ) {

	global $jigoshop_body_classes;

	$jigoshop_body_classes = (array) $jigoshop_body_classes;

	$jigoshop_body_classes = array_unique( array_merge( $class, $jigoshop_body_classes ));
}

function jigoshop_body_class($classes) {

	global $jigoshop_body_classes;

	$jigoshop_body_classes = (array) $jigoshop_body_classes;

	$classes = array_unique( array_merge( $classes, $jigoshop_body_classes ));

	return $classes;
}
add_filter('body_class','jigoshop_body_class');

//### Extra Review Field in comments #########################################################

function jigoshop_add_comment_rating($comment_id) {
	if ( isset($_POST['rating']) ) :
		if (!$_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0) $_POST['rating'] = 5;
		add_comment_meta( $comment_id, 'rating', $_POST['rating'], true );
	endif;
}
add_action( 'comment_post', 'jigoshop_add_comment_rating', 1 );

function jigoshop_check_comment_rating($comment_data) {
	// If posting a comment (not trackback etc) and not logged in
	if ( isset($_POST['rating']) && !jigoshop::verify_nonce('comment_rating') )
		wp_die( __('You have taken too long. Please go back and refresh the page.', 'jigoshop') );

	elseif ( isset($_POST['rating']) && empty($_POST['rating']) && $comment_data['comment_type']== '' ) {
		wp_die( __('Please rate the product.',"jigowatt") );
		exit;
	}
	return $comment_data;
}
add_filter('preprocess_comment', 'jigoshop_check_comment_rating', 0);

//### Comments #########################################################

function jigoshop_comments($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment; global $post; ?>

	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
		<div id="comment-<?php comment_ID(); ?>" class="comment_container">

  			<?php echo get_avatar( $comment, $size='60' ); ?>

			<div class="comment-text">
				<?php if ( $rating = get_comment_meta( $comment->comment_ID, 'rating', true ) ): ?>
				<div class="star-rating" title="<?php echo esc_attr( $rating ); ?>">
					<span style="width:<?php echo $rating*16; ?>px"><?php echo $rating; ?> <?php _e('out of 5', 'jigoshop'); ?></span>
				</div>
				<?php endif; ?>
				<?php if ($comment->comment_approved == '0') : ?>
					<p class="meta"><em><?php _e('Your comment is awaiting approval','jigoshop'); ?></em></p>
				<?php else : ?>
					<p class="meta">
						<?php _e('Rating by','jigoshop'); ?> <strong class="reviewer vcard"><span class="fn"><?php comment_author(); ?></span></strong> <?php _e('on','jigoshop'); ?> <?php echo date_i18n(get_option('date_format'), strtotime(get_comment_date('Y-m-d'))); ?>:
					</p>
				<?php endif; ?>
  				<div class="description"><?php comment_text(); ?></div>
  				<div class="clear"></div>
  			</div>
			<div class="clear"></div>
		</div>
	<?php
}

//### Exclude order comments from front end #########################################################
add_filter( 'comments_clauses', 'jigoshop_exclude_order_admin_comments', 10, 1);
function jigoshop_exclude_order_admin_comments( $clauses ) {

	// NOTE: bit of a hack, tests if we're in the admin & its an ajax call
	// Don't hide when viewing orders in admin
	if (is_admin() && is_ajax()) {

		return $clauses;
	}

	// Hide all those comments which aren't of type jigoshop
	$clauses['where'] .= ' AND comment_type != "jigoshop"';

	return $clauses;
}

/**
 * Support for Import/Export
 *
 * WordPress import should work - however, it fails to import custom product attribute taxonomies.
 * This code grabs the file before it is imported and ensures the taxonomies are created.
 **/
function jigoshop_import_start() {

	global $wpdb;

	$id = (int) $_POST['import_id'];
	$file = get_attached_file( $id );

	$parser = new WXR_Parser();
	$import_data = $parser->parse( $file );

	if (isset($import_data['posts'])) :
		$posts = $import_data['posts'];

		if ($posts && sizeof($posts)>0) foreach ($posts as $post) :

			if ($post['post_type']=='product') :

				if ($post['terms'] && sizeof($post['terms'])>0) :

					foreach ($post['terms'] as $term) :

						$domain = $term['domain'];

						if (strstr($domain, 'pa_')) :

							// Make sure it exists!
							if (!taxonomy_exists( $domain )) :

								$nicename = sanitize_title(str_replace('pa_', '', $domain));

								$exists_in_db = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_id FROM ".$wpdb->prefix . "jigoshop_attribute_taxonomies WHERE attribute_name = %s;", $nicename ) );

								// Create the taxonomy
								if (!$exists_in_db) :
									$wpdb->insert( $wpdb->prefix . "jigoshop_attribute_taxonomies", array( 'attribute_name' => $nicename, 'attribute_type' => 'select' ), array( '%s', '%s' ) );
								endif;

								// Register the taxonomy now so that the import works!
								register_taxonomy( $domain,
							        array('product'),
							        array(
							            'hierarchical' => true,
							            'labels' => array(
												'name'             => $nicename,
												'singular_name'    => $nicename,
												'search_items'     =>  __( 'Search ', 'jigoshop') . $nicename,
												'all_items'        => __( 'All ', 'jigoshop') . $nicename,
												'parent_item'      => __( 'Parent ', 'jigoshop') . $nicename,
												'parent_item_colon'=> __( 'Parent ', 'jigoshop') . $nicename . ':',
												'edit_item'        => __( 'Edit ', 'jigoshop') . $nicename,
												'update_item'      => __( 'Update ', 'jigoshop') . $nicename,
												'add_new_item'     => __( 'Add New ', 'jigoshop') . $nicename,
												'new_item_name'    => __( 'New ', 'jigoshop') . $nicename
							            ),
										'show_ui'  => false,
										'query_var'=> true,
										'rewrite'  => array( 'slug'=> sanitize_title($nicename), 'with_front'=> false, 'hierarchical'=> true ),
							        )
							    );

								update_option('jigowatt_update_rewrite_rules', '1');

							endif;

						endif;

					endforeach;

				endif;

			endif;

		endforeach;

	endif;

}
add_action('import_start', 'jigoshop_import_start');

/**
 * Support for Jigoshop being the very first plugin loaded by WordPress
 *
 * Will only happen once a user has 'activated' a (any) plugin.
 *
 * Ensures our sessions are run first.
 * Runs last on the 'activated_plugin' hook due to a low priority.
 *
 **/
function jigoshop_plugin_loads_first() {
	// ensure path to this file is via main wp plugin path
	$wp_path_to_this_file = preg_replace( '/(.*)plugins\/(.*)$/', WP_PLUGIN_DIR."/$2", __FILE__ );
	$this_plugin = plugin_basename( trim( $wp_path_to_this_file ));
	$active_plugins = get_option( 'active_plugins' );
	$this_plugin_key = array_search( $this_plugin, $active_plugins );
	if ( $this_plugin_key ) { // if it's 0 it's the first plugin already, no need to continue
		array_splice( $active_plugins, $this_plugin_key, 1 );
		array_unshift( $active_plugins, $this_plugin );
		update_option( 'active_plugins', $active_plugins );
	}
}
// disconnecting for further testing, breaks 'lazy loader plugins' (-JAP-)
//add_action( 'activated_plugin', 'jigoshop_plugin_loads_first', 999 );


if(!function_exists('jigoshop_log')){

    /**
     * Logs to the debug log when you enable wordpress debug mode.
     *
     * @param string $from_class is the name of the php file that you are logging from.
     * defaults to jigoshop if non is supplied.
     * @param mixed $message this can be a regular string, array or object
     */
    function jigoshop_log( $message, $from_class = 'jigoshop' ) {

        if( WP_DEBUG === true ) :
            if( is_array( $message ) || is_object( $message ) ) :
                error_log( $from_class . ': ' . print_r( $message, true ) );
            else :
                error_log( $from_class . ': ' . $message );
            endif;
        endif;

    }
}
